<template>
	<view style="padding: 30rpx;">
		<view>
			<view>
				<view v-for="item in businessList" :key="item.buissness.id" style="margin-top: 20rpx;">
					<uni-card isFull="true">
						<view style="display: flex; grid-gap: 30rpx; margin-bottom: 10rpx;"
							@click="goToDetail(item.buissness.id)">
							<view style="width: 30%;">
								<image :src="item.buissness.avatar"
									style="width: 100%; height: 200rpx; border-radius: 10rpx; display: block;"></image>
							</view>
							<view
								style="flex: 1; display: flex; flex-direction: column; justify-content: space-between; grid-gap: 10rpx;">
								<view style="font-size: 36rpx; font-weight: bold;">{{ item.buissness.name }}</view>

								<view style="display: flex;  color: #666; ">
									<view style="flex: 1;">
										<text style="color: #ff9800; font-weight: bold;">{{ item.buissness.score }}分</text>
										<text style="margin-left: 10rpx;">已售:{{ item.buissness.nums }}</text>
									</view>
									<view style="flex: 1; text-align: right; font-size: 30rpx;">30分钟内送达</text>
									</view>
								</view>

								<view style="color: skyblue;">免配送费</view>
								<view style="width: 30%;">
									<image :src="item.good.img"
										style="width: 100%; height: 150rpx; border-radius: 10rpx; display: block;"></image>
									<text>{{item.good.name}}</text>
										
								</view>
							</view>
						</view>
					</uni-card>
				</view>

			</view>


		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				name: '',
				businessList: [],
				goodsList:[],
			}
		},
		onLoad(option) {
			this.name = option.name
			this.load()
			console.log(this.businessList)
		},
		methods: {
			goToDetail(businessId){
				console.log(businessId)
					uni.navigateTo({
						url: '/pages/detail/detail?businessId='+businessId
					})
			},
			 async  load() {
				
					await this.getGood();
					console.log(this.goodsList)
					
					if(this.goodsList.length>0){
						for (const good of this.goodsList) {  
							await this.getBusiness(good.businessName)
						}
					}
					console.log(this.businessList)
					const  ceshi = this.businessList.map((element, index) => {
					  return {  
					    buissness: element,  
					    good: this.goodsList[index]  
					  };  
					});
					this.businessList=ceshi
			},
			async getBusiness(name) {  
			  try {  
			    const res = await this.$request.get('/business/selectAll', {  
			      name: name,  
			      status: '通过'  
			    });  
			    const businessInfo = res.data || [];  
			    // 更新商家列表  
			    this.businessList = this.businessList.concat(businessInfo);  
			  } catch (error) {  
			    // 处理请求错误  
			    console.error('获取商家信息时出错:', error);  
			  }  
			}  ,
			  
			async getGood() {  
			  try {  
			    const res = await this.$request.get('/goods/selectAll', {  
			      name: this.name,  
			      status: '通过'  
			    });  
			    this.goodsList = res.data || [];  
			  } catch (error) {  
			    // 处理请求错误  
			    console.error('获取商品信息时出错:', error);  
			  }  
			}
			
		}
	}
</script>

<style>
	.category-item {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		align-items: center;
		grid-gap: 10rpx;
	}
</style>